package com.ysd.Dao.Impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.ProductSeries;
import com.ysd.entity.User;
import com.ysd.util.DBmanager;

public class UserDaoImpl {
	/**
	 * 添加留言
	 * 
	 * @param user
	 * @return
	 */
	public int addMessage(User user) {
		String sql = "INSERT INTO user(userName,userPhone,userEmail,messageContent) VALUES('" + user.getUserName()+ "','" + user.getUserPhone() + "','" + user.getUserEmail() + "','" + user.getMessageContent() + "')";
		System.out.println(sql);
		return DBmanager.executeUpdate(sql);
	}

	/**
	 * 查询全部留言长度
	 * 
	 * @return
	 */
	public List<User> getAllMessage(String status) {
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		List<User> list = new ArrayList<User>();
		conn = DBmanager.getConnection();
		try {
			st = conn.createStatement();
			String sql = "select * from user where 1=1 ";
			if (status != null && !status.equals("")) {
				sql += status = "and status='" + status + "'";
			}
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int messageId = rs.getInt("messageId");
				String userName = rs.getString("userName");
				String userPhone = rs.getString("userPhone");
				String userEmail = rs.getString("userEmail");
				String messageContent = rs.getString("messageContent");
				String time = rs.getString("time");
				String sta = rs.getString("status");
				User user = new User(messageId, userName, userPhone, userEmail, messageContent, time, sta);
				list.add(user);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBmanager.closeAll(conn, st, rs);
		}

	}

	/**
	 * 按状态分页查询留言全部留言
	 * 
	 * @return
	 */
	public List<User> getMessageByStatus(String status, int page, int size) {
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		List<User> list = new ArrayList<User>();
		conn = DBmanager.getConnection();
		try {
			st = conn.createStatement();
			String sql = "select * from user where 1=1 ";
			if (status != null && !status.equals("")) {
				sql += status = "and status='" + status + "'";
			}
			sql += "order by time desc limit " + page + "," + size;
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int messageId = rs.getInt("messageId");
				String userName = rs.getString("userName");
				String userPhone = rs.getString("userPhone");
				String userEmail = rs.getString("userEmail");
				String messageContent = rs.getString("messageContent");
				String time = rs.getString("time");
				String sta = rs.getString("status");
				User user = new User(messageId, userName, userPhone, userEmail, messageContent, time, sta);
				list.add(user);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBmanager.closeAll(conn, st, rs);
		}

	}
	/**
	 * 根据留言id查询留言
	 * 
	 * @return
	 */
	public User getMessageById(int mId) {
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		User user =null;
		conn = DBmanager.getConnection();
		try {
			st = conn.createStatement();
			String sql = "select * from user where messageId='"+mId+"'";
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int messageId = rs.getInt("messageId");
				String userName = rs.getString("userName");
				String userPhone = rs.getString("userPhone");
				String userEmail = rs.getString("userEmail");
				String messageContent = rs.getString("messageContent");
				String time = rs.getString("time");
				String sta = rs.getString("status");
				String reply = rs.getString("reply");
				 user = new User(messageId, userName, userPhone, userEmail, messageContent, time, sta,reply);
			}
			return user;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBmanager.closeAll(conn, st, rs);
		}

	}
	/**
	 * 
	 * 根据留言id修改留言状态
	 * user前台传来的参数
	 * 
	 * @author 86176 return 返回受影响行数
	 */
	public int updateMessageById(User user) {
		return DBmanager.executeUpdate("update user set status='"+user.getStatus()+"'  where messageId ="+user.getMessageId());
	}
	/**
	 * 
	 * 根据留言id修改留言状态
	 * user前台传来的参数
	 * 
	 * @author 86176 return 返回受影响行数
	 */
	public int updateMessageReplyById(User user) {
		return DBmanager.executeUpdate("update user set status='已回复' , reply='"+user.getReply()+"' where messageId ="+user.getMessageId());
	}
	/**
	 * 
	 * 根据留言id删除留言
	 *  mId前台传来的参数
	 * @author 86176 return 返回受影响行数
	 */
	public int deleteMessageById(int mId) {
		return DBmanager.executeUpdate("DELETE FROM user where messageId =" + mId);
	}
	/**
	 * 
	 * 根据多条留言id删除留言
	 *  ids前台传来的参数
	 * 
	 * @author 86176 return 返回受影响行数
	 */
	public int deleteMessageByIds(String ids) {
		return DBmanager.executeUpdate("DELETE from user where messageId in("+ids+")");
	}
}
